### Replication code for "Family Matters?"
### Summer 2018
### See the readme file for more details on what goes where in this replication package
### Contact Ariel White with questions: arwhi@mit.edu

#load main deidentified data 
library(data.table)
load("main_householdsmerged_deidentified.Rdata") #"smallneighborsfull2" 

#few time subsets
sn09 <- smallneighborsfull2[firstcase > "2008-11-04",] #everybody after the 2008 election
snplacebo <- smallneighborsfull2[firstcase >  "2012-11-06",] #everybody after the 2012 election


###########################################################################
# some descriptive stats
###########################################################################

# describe just 2012 matches (for slides)
sn2012 <- smallneighborsfull2[firstcase >=  "2012-01-01"& firstcase <=  "2012-12-31"]; dim(sn2012)
length(unique(sn2012$def_spn))
sn201213 <- smallneighborsfull2[firstcase >=  "2012-01-01"& firstcase <=  "2013-12-31"]; dim(sn201213)
length(unique(sn201213$def_spn))

#how many saw hh member jailed?
sum(smallneighborsfull2$anyjail)


###########################################################################
# simplest observational analysis
###########################################################################

library(multiwayvcov); library(lmtest)
library(stargazer)

#conviction
simplestols09 <- lm(vote2012 ~anyconv, data=sn09); summary(simplestols09)
simplest09.vcovCL<-cluster.vcov(simplestols09, sn09$def_spn)
coeftest(simplestols09, simplest09.vcovCL) 
simpleols09 <- lm(vote2012 ~anyconv+vote2008+black + male +voter_male+voter_age, data=sn09); summary(simpleols09)
simple09.vcovCL<-cluster.vcov(simpleols09, sn09$def_spn)
coeftest(simpleols09, simple09.vcovCL) 

#and include jail too?
simplestols09 <- lm(vote2012 ~anyjail, data=sn09); summary(simplestols09)
simplest09.vcovCL<-cluster.vcov(simplestols09, sn09$def_spn)
coeftest(simplestols09, simplest09.vcovCL) 
simpleols09 <- lm(vote2012 ~anyjail+vote2004+ vote2006+vote2008+black + male +voter_male+voter_age, data=sn09); summary(simpleols09)
simple09.vcovCL<-cluster.vcov(simpleols09, sn09$def_spn)
coeftest(simpleols09, simple09.vcovCL) 

simpleols09j <- lm(vote2012 ~anyconv+vote2004+ vote2006+vote2008+black + male +voter_male+voter_age, data=sn09); summary(simpleols09)
simple09j.vcovCL<-cluster.vcov(simpleols09j, sn09$def_spn)
coeftest(simpleols09j, simple09j.vcovCL) 

simpleols09cj <- lm(vote2012 ~anyjail+ anyconv+vote2004+vote2006+vote2008+black + male +voter_male+voter_age, data=sn09); summary(simpleols09cj)
simple09cj.vcovCL<-cluster.vcov(simpleols09cj, sn09$def_spn)
coeftest(simpleols09cj, simple09cj.vcovCL) 

stargazer(simplestols09, simpleols09, simpleols09j, simpleols09cj,label= "naiveols09_conv", align=T, omit.stat=c("LL","ser","f"), title="Basic OLS estimates, including prior vote, defendant and voter characteristics", covariate.labels=c("Household Member Jailed", "Household Member Convicted", "2004 Turnout", "2006 Turnout", "2008 Turnout",  "HH Member Black", "HH Member Male", "Voter Male", "Voter Age (Years)"), dep.var.labels= c("Voted 2012"),
se=list(coeftest(simplestols09, simplest09.vcovCL)[,2] , coeftest(simpleols09, simple09.vcovCL)[,2], coeftest(simpleols09j, simple09j.vcovCL)[,2], coeftest(simpleols09cj, simple09cj.vcovCL)[,2] ), #swap in defendant-clustered SE's 
out="smallneighbors09convjail_naiveols.tex",star.cutoffs=c(0.05), notes="$^{*}$p$<$0.05", notes.append=FALSE) 
#actually, I don't think this table is in the final paper-- SI1.1 now includes people without a HH member arrested as well. 

#Run a placebo test: do 2013-14 cases have an "effect" on HH member voting in 2012? (bad sign) 
simplestols09 <- lm(vote2012 ~anyjail, data=snplacebo); summary(simplestols09)
simplest09.vcovCL<-cluster.vcov(simplestols09, snplacebo$def_spn)
coeftest(simplestols09, simplest09.vcovCL) 
simpleols09 <- lm(vote2012 ~anyjail+vote2004+vote2006+vote2008+black + male +voter_male+voter_age, data=snplacebo); summary(simpleols09)
simple09.vcovCL<-cluster.vcov(simpleols09, snplacebo$def_spn)
coeftest(simpleols09, simple09.vcovCL) 

simpleols09j <- lm(vote2012 ~anyconv+vote2004+vote2006+vote2008+black + male +voter_male+voter_age, data=snplacebo); summary(simpleols09)
simple09j.vcovCL<-cluster.vcov(simpleols09j, snplacebo$def_spn)
coeftest(simpleols09j, simple09j.vcovCL) 

simpleols09cj <- lm(vote2012 ~anyjail+ anyconv+vote2004+vote2006+vote2008+black + male +voter_male+voter_age, data=snplacebo); summary(simpleols09)
simple09cj.vcovCL<-cluster.vcov(simpleols09cj, snplacebo$def_spn)
coeftest(simpleols09cj, simple09cj.vcovCL) 

#this table also isn't in the final version. 
#library(stargazer)
#stargazer(simplestols09, simpleols09, simpleols09j, simpleols09cj,label= "naiveols09_placebo", align=T, omit.stat=c("LL","ser","f"), title="Placebo Tests for Basic OLS Specifications (Post-Election Cases)", covariate.labels=c("Household Member Jailed", "Household Member Convicted", "2004 Turnout", "2006 Turnout", "2008 Turnout",  "HH Member Black", "HH Member Male", "Voter Male", "Voter Age (Years)"), dep.var.labels= c("Voted 2012"),
#se=list(coeftest(simplestols09, simplest09.vcovCL)[,2] , coeftest(simpleols09, simple09.vcovCL)[,2], coeftest(simpleols09j, simple09j.vcovCL)[,2], coeftest(simpleols09cj, simple09cj.vcovCL)[,2] ), #swap in defendant-clustered SE's 
#out="smallneighbors09convjail_naiveols_placebo.tex",star.cutoffs=c(0.05), notes="$^{*}$p$<$0.05", notes.append=FALSE) 

##########################################################################
#ID strategy: look at people whose HH members were charged right before/after the election

#start with just one month around election day
around1m <-  smallneighborsfull2[smallneighborsfull2$firstcase <= "2012-12-06" & smallneighborsfull2$firstcase > "2012-10-06", ]; dim(around1m)
around1m[,chargebefore:=0]; around1m[firstcase < "2012-11-06",chargebefore:=1] #set up some treatment-timing indicators

#set it up as a factor and then do the interaction with timing? 
around1m[,casebefore:=0]; around1m[firstcase < "2012-11-06",casebefore:=1] 
around1m[anyconv==0,treatment:="chargeonly"] 
around1m[anyconv==1 & anyjail==0,treatment:="convonly"] 
around1m[anyjail==1,treatment:="jail"] 
around1m$treatfac <- as.factor(around1m$treatment)

with(around1m, tapply(vote2012, list(casebefore, treatfac), mean))

ba3 <- lm(vote2012 ~ casebefore + treatfac + casebefore*treatfac, data=around1m); summary(ba3)
ba3.vcovCL<-cluster.vcov(ba3, around1m$def_spn)
coeftest(ba3, ba3.vcovCL) 

ba3c <- lm(vote2012 ~ casebefore + treatfac + casebefore*treatfac+voter_male+voter_age + male + black + vote2006+ vote2008 +vote2010, data=around1m); summary(ba3c)
ba3c.vcovCL<-cluster.vcov(ba3c, around1m$def_spn)
coeftest(ba3c, ba3c.vcovCL) 

stargazer(ba3, ba3c,label= "beforeafter_1m_factor",order=c(1, 2, 3, 11, 12, 4, 5, 6, 7, 8, 9, 10,13),align=T, omit.stat=c("LL","ser","f"), title="Proximal Contact on Voting (Using Case Timing)", covariate.labels=c("Case Before Election", "HH Member Convicted", "HH Member Jailed", "HH Member Convicted * Before Election", "HH Member Jailed * Before Election","Voter Male", "Voter Age (Years)", "HH Member Male", "HH Member Black", "2006 Turnout", "2008 Turnout", "2010 Turnout" ),
dep.var.labels= c("Voted 2012"),
se=list(coeftest(ba3, ba3.vcovCL) [,2] , coeftest(ba3c, ba3c.vcovCL) [,2]), #swap in defendant-clustered SE's 
out="beforeafter_1m_factorsetup.tex",star.cutoffs=c(0.05), notes="$^{*}$p$<$0.05", notes.append=FALSE)
#Table 2


#do this at a range of time windows, for all three of these outcomes (just charge, just conv, jail)
ed2012 <- as.Date("2012-11-06") #election day

smallneighborsfull2[,chargebefore:=0]; smallneighborsfull2[firstcase < "2012-11-06" & anyconv==0,chargebefore:=1] 
smallneighborsfull2[,convbefore:=0]; smallneighborsfull2[firstcase < "2012-11-06" & anyconv==1 & anyjail==0,convbefore:=1] 
smallneighborsfull2[,jailbefore:=0]; smallneighborsfull2[firstcase < "2012-11-06" & anyconv==1 & anyjail==1,jailbefore:=1] 
smallneighborsfull2[,anyjail30:=0];smallneighborsfull2[totalsente >= 30, anyjail30:=1] #also look at longer jail sentences (>30 days)
smallneighborsfull2[,jail30before:=0]; smallneighborsfull2[firstcase < "2012-11-06" & anyconv==1 & anyjail30==1,jail30before:=1] 

weeks <- 1*40
storage <- as.data.frame(matrix(NA, nrow=weeks, ncol=17))
for (i in 2:weeks){
	lower <- ed2012 - i*7; higher <- ed2012 + i*7 #add/subtract off days to get the window
	window <- smallneighborsfull2[firstcase < higher & firstcase > lower, ]
	#window <- window[!(firstcase > ed2012-3 & firstcase < ed2012+1), ] #can also drop the few days just prior to election,where it's hard to attribute treatment-- doesn't make much difference. 
	ba1 <- lm(vote2012 ~ chargebefore, data=window[anyconv==0]); summary(ba1) #estimate just-charge model
	ba1.vcovCL<-cluster.vcov(ba1, window[anyconv==0]$def_spn)
	ba1conv <- lm(vote2012 ~ convbefore, data=window[anyconv==1 & anyjail==0,]); summary(ba1) #just-conv
	ba1conv.vcovCL<-cluster.vcov(ba1conv, window[anyconv==1& anyjail==0,]$def_spn)
	ba1jail <- lm(vote2012 ~ jailbefore, data=window[anyjail==1,]); summary(ba1) #jail
	ba1jail.vcovCL<-cluster.vcov(ba1jail, window[anyjail==1,]$def_spn)

	ba1jail30 <- lm(vote2012 ~ jail30before, data=window[anyjail30==1,]); summary(ba1) #jail (30+days)
	ba1jail30.vcovCL<-cluster.vcov(ba1jail30, window[anyjail30==1,]$def_spn)


	storage[i,1] <- nrow(window) 
	storage[i,2] <- est <- coeftest(ba1, ba1.vcovCL)[2,1]
	storage[i,3] <- p <- coeftest(ba1, ba1.vcovCL)[2,4]
	err<- coeftest(ba1, ba1.vcovCL)[2,2]; dof <- ba1$df
	storage[i,4] <- est + -1*err*qt(0.975, dof) 
	storage[i,5] <- est + 1*err*qt(0.975, dof) 

	storage[i,6] <- est <- coeftest(ba1conv, ba1conv.vcovCL)[2,1]
	storage[i,7] <- p <- coeftest(ba1conv, ba1conv.vcovCL)[2,4]
	err<- coeftest(ba1conv, ba1conv.vcovCL)[2,2]; dof <- ba1conv$df
	storage[i,8] <- est + -1*err*qt(0.975, dof) 
	storage[i,9] <- est + 1*err*qt(0.975, dof) 

	storage[i,10] <- est <- coeftest(ba1jail, ba1jail.vcovCL)[2,1]
	storage[i,11] <- p <- coeftest(ba1jail, ba1jail.vcovCL)[2,4]
	err<- coeftest(ba1jail, ba1jail.vcovCL)[2,2]; dof <- ba1jail$df
	storage[i,12] <- est + -1*err*qt(0.975, dof) 
	storage[i,13] <- est + 1*err*qt(0.975, dof) 

	storage[i,14] <- est <- coeftest(ba1jail30, ba1jail30.vcovCL)[2,1]
	storage[i,15] <- p <- coeftest(ba1jail30, ba1jail30.vcovCL)[2,4]
	err<- coeftest(ba1jail30, ba1jail30.vcovCL)[2,2]; dof <- ba1jail30$df
	storage[i,16] <- est + -1*err*qt(0.975, dof) 
	storage[i,17] <- est + 1*err*qt(0.975, dof)
}
colnames(storage) <- c("n", "case_est", "case_p", "case_lowCI", "case_highCI", "conv_est", "conv_p", "conv_lowCI", "conv_highCI", "jail_est", "jail_p", "jail_lowCI", "jail_highCI",  "jail30_est", "jail30_p", "jail30_lowCI", "jail30_highCI")
storage$row <- 1:nrow(storage)

#make plots for Figure 2 
pdf("beforeafter_justcharge_windows40.pdf")
plot(storage$row, storage$case_est, ylim=c(-.3, .1), pch=19,main= "Effect of HH Member Misdemeanor Charge on Voting (Varying Windows)", xlab="Weeks Around Election", ylab="Change in 2012 turnout (percentage points)")
abline(h=0, lty=2, col=gray(.4))
segments(storage$row, storage$case_lowCI, storage$row, storage$case_highCI, col="dodgerblue4")
dev.off()

pdf("beforeafter_justconv_windows40.pdf")
plot(storage$row, storage$conv_est, ylim=c(-.3, .1), pch=19, main= "Effect of HH Member Conviction on Voting (Varying Windows)", xlab="Weeks Around Election", ylab="Change in 2012 turnout (percentage points)")
abline(h=0, lty=2, col=gray(.4))
segments(storage$row, storage$conv_lowCI, storage$row, storage$conv_highCI, col="dodgerblue4")
dev.off()

pdf("beforeafter_jail_windows40.pdf")
plot(storage$row, storage$jail_est, ylim=c(-.3, .1), pch=19, main= "Effect of HH Member Jail Sentence on Voting (Varying Windows)", xlab="Weeks Around Election", ylab="Change in 2012 turnout (percentage points)")
abline(h=0, lty=2, col=gray(.4))
segments(storage$row, storage$jail_lowCI, storage$row, storage$jail_highCI, col="dodgerblue4")
dev.off()

pdf("beforeafter_justcharge_windows40_slides.pdf") #bigger text
plot(storage$row, storage$case_est, ylim=c(-.3, .1), pch=19,#main= "Effect of HH Member Misdemeanor Charge on Voting (Varying Windows)",
 xlab="Weeks Around Election", ylab="Change in 2012 turnout (percentage points)", cex.axis=1.5,cex.main=2, cex.lab=1.5)
abline(h=0, lty=2, col=gray(.4))
segments(storage$row, storage$case_lowCI, storage$row, storage$case_highCI, col="dodgerblue4")
dev.off()

pdf("beforeafter_justconv_windows40_slides.pdf")
plot(storage$row, storage$conv_est, ylim=c(-.3, .1), pch=19, #main= "Effect of HH Member Conviction on Voting (Varying Windows)",
 xlab="Weeks Around Election", ylab="Change in 2012 turnout (percentage points)", cex.axis=1.5,cex.main=2, cex.lab=1.5)
abline(h=0, lty=2, col=gray(.4))
segments(storage$row, storage$conv_lowCI, storage$row, storage$conv_highCI, col="dodgerblue4")
dev.off()

pdf("beforeafter_jail_windows40_slides.pdf")
plot(storage$row, storage$jail_est, ylim=c(-.3, .1), pch=19, #main= "Effect of HH Member Jail Sentence on Voting (Varying Windows)",
 xlab="Weeks Around Election", ylab="Change in 2012 turnout (percentage points)", cex.axis=1.5,cex.main=2, cex.lab=1.5)
abline(h=0, lty=2, col=gray(.4))
segments(storage$row, storage$jail_lowCI, storage$row, storage$jail_highCI, col="dodgerblue4")
dev.off()

pdf("beforeafter_jail30_windows40_slides.pdf")
plot(storage$row, storage$jail30_est, ylim=c(-.3, .1), pch=19, #main= "Effect of HH Member Jail Sentence on Voting (Varying Windows)",
 xlab="Weeks Around Election", ylab="Change in 2012 turnout (percentage points)", cex.axis=1.5,cex.main=2, cex.lab=1.5)
abline(h=0, lty=2, col=gray(.4))
segments(storage$row, storage$jail30_lowCI, storage$row, storage$jail30_highCI, col="dodgerblue4")
dev.off()


##and now do this again, but dropping the month around the election 
weeks <- 1*40
storage <- as.data.frame(matrix(NA, nrow=weeks, ncol=13))
for (i in 5:weeks){
	lower <- ed2012 - i*7; higher <- ed2012 + i*7 #add/subtract off days to get the window
	window <- smallneighborsfull2[firstcase < higher & firstcase > lower, ]
	
	window <- window[!(firstcase > ed2012-30 & firstcase < ed2012+30), ] #drop the month before
	ba1 <- lm(vote2012 ~ chargebefore, data=window[anyconv==0]); summary(ba1) #est. just-charge model
	ba1.vcovCL<-cluster.vcov(ba1, window[anyconv==0]$def_spn)
	ba1conv <- lm(vote2012 ~ convbefore, data=window[anyconv==1 & anyjail==0,]); summary(ba1) #just-conv
	ba1conv.vcovCL<-cluster.vcov(ba1conv, window[anyconv==1& anyjail==0,]$def_spn)
	ba1jail <- lm(vote2012 ~ jailbefore, data=window[anyjail==1,]); summary(ba1) #jail
	ba1jail.vcovCL<-cluster.vcov(ba1jail, window[anyjail==1,]$def_spn)

	storage[i,1] <- nrow(window) 
	storage[i,2] <- est <- coeftest(ba1, ba1.vcovCL)[2,1]
	storage[i,3] <- p <- coeftest(ba1, ba1.vcovCL)[2,4]
	err<- coeftest(ba1, ba1.vcovCL)[2,2]; dof <- ba1$df
	storage[i,4] <- est + -1*err*qt(0.975, dof) 
	storage[i,5] <- est + 1*err*qt(0.975, dof) 

	storage[i,6] <- est <- coeftest(ba1conv, ba1conv.vcovCL)[2,1]
	storage[i,7] <- p <- coeftest(ba1conv, ba1conv.vcovCL)[2,4]
	err<- coeftest(ba1conv, ba1conv.vcovCL)[2,2]; dof <- ba1conv$df
	storage[i,8] <- est + -1*err*qt(0.975, dof) 
	storage[i,9] <- est + 1*err*qt(0.975, dof) 

	storage[i,10] <- est <- coeftest(ba1jail, ba1jail.vcovCL)[2,1]
	storage[i,11] <- p <- coeftest(ba1jail, ba1jail.vcovCL)[2,4]
	err<- coeftest(ba1jail, ba1jail.vcovCL)[2,2]; dof <- ba1jail$df
	storage[i,12] <- est + -1*err*qt(0.975, dof) 
	storage[i,13] <- est + 1*err*qt(0.975, dof) 
}
colnames(storage) <- c("n", "case_est", "case_p", "case_lowCI", "case_highCI", "conv_est", "conv_p", "conv_lowCI", "conv_highCI", "jail_est", "jail_p", "jail_lowCI", "jail_highCI")
storage$row <- 1:nrow(storage)

#Figure 3
pdf("beforeafter_justconv_windows40_drop30.pdf")
plot(storage$row, storage$conv_est, ylim=c(-.3, .1), pch=19, main= "Effect of HH Member Conviction on Voting (Varying Windows)", xlab="Weeks Around Election", ylab="Change in 2012 turnout (percentage points)")
abline(h=0, lty=2, col=gray(.4))
segments(storage$row, storage$conv_lowCI, storage$row, storage$conv_highCI, col="dodgerblue4")
dev.off()

pdf("beforeafter_justconv_windows40_drop30_slides.pdf") #adjust size of text
plot(storage$row, storage$conv_est, ylim=c(-.3, .1), pch=19, #main= "Effect of HH Member Conviction on Voting (Varying Windows)",
 xlab="Weeks Around Election", ylab="Change in 2012 turnout (percentage points)", cex.axis=1.5,cex.main=2, cex.lab=1.5)
abline(h=0, lty=2, col=gray(.4))
segments(storage$row, storage$conv_lowCI, storage$row, storage$conv_highCI, col="dodgerblue4")
dev.off()

####include covars (robustness) 
weeks <- 1*40
storage <- as.data.frame(matrix(NA, nrow=weeks, ncol=13))
for (i in 2:weeks){
	lower <- ed2012 - i*7; higher <- ed2012 + i*7 #add/subtract off days to get the window
	window <- smallneighborsfull2[firstcase < higher & firstcase > lower, ]
	ba1 <- lm(vote2012 ~ chargebefore+voter_male+voter_age + male + black + vote2006+ vote2008 +vote2010, data=window[anyconv==0]); summary(ba1) #est. just-charge model
	ba1.vcovCL<-cluster.vcov(ba1, window[anyconv==0]$def_spn)
	ba1conv <- lm(vote2012 ~ convbefore+voter_male+voter_age + male + black + vote2006+ vote2008 +vote2010, data=window[anyconv==1 & anyjail==0,]); summary(ba1) #just-conv
	ba1conv.vcovCL<-cluster.vcov(ba1conv, window[anyconv==1& anyjail==0,]$def_spn)
	ba1jail <- lm(vote2012 ~ jailbefore+voter_male+voter_age + male + black + vote2006+ vote2008 +vote2010, data=window[anyjail==1,]); summary(ba1) #jail
	ba1jail.vcovCL<-cluster.vcov(ba1jail, window[anyjail==1,]$def_spn)

	storage[i,1] <- nrow(window) 
	storage[i,2] <- est <- coeftest(ba1, ba1.vcovCL)[2,1]
	storage[i,3] <- p <- coeftest(ba1, ba1.vcovCL)[2,4]
	err<- coeftest(ba1, ba1.vcovCL)[2,2]; dof <- ba1$df
	storage[i,4] <- est + -1*err*qt(0.975, dof) 
	storage[i,5] <- est + 1*err*qt(0.975, dof) 

	storage[i,6] <- est <- coeftest(ba1conv, ba1conv.vcovCL)[2,1]
	storage[i,7] <- p <- coeftest(ba1conv, ba1conv.vcovCL)[2,4]
	err<- coeftest(ba1conv, ba1conv.vcovCL)[2,2]; dof <- ba1conv$df
	storage[i,8] <- est + -1*err*qt(0.975, dof) 
	storage[i,9] <- est + 1*err*qt(0.975, dof) 

	storage[i,10] <- est <- coeftest(ba1jail, ba1jail.vcovCL)[2,1]
	storage[i,11] <- p <- coeftest(ba1jail, ba1jail.vcovCL)[2,4]
	err<- coeftest(ba1jail, ba1jail.vcovCL)[2,2]; dof <- ba1jail$df
	storage[i,12] <- est + -1*err*qt(0.975, dof) 
	storage[i,13] <- est + 1*err*qt(0.975, dof) 
}
colnames(storage) <- c("n", "case_est", "case_p", "case_lowCI", "case_highCI", "conv_est", "conv_p", "conv_lowCI", "conv_highCI", "jail_est", "jail_p", "jail_lowCI", "jail_highCI")
storage$row <- 1:nrow(storage)

pdf("beforeafter_justcharge_windows40_covars.pdf")
plot(storage$row, storage$case_est, ylim=c(-.3, .1), pch=19,main= "Effect of HH Member Misdemeanor Charge on Voting (Varying Windows)", xlab="Weeks Around Election", ylab="Change in 2012 turnout (percentage points)")
abline(h=0, lty=2, col=gray(.4))
segments(storage$row, storage$case_lowCI, storage$row, storage$case_highCI, col="dodgerblue4")
dev.off()

pdf("beforeafter_justconv_windows40_covars.pdf")
plot(storage$row, storage$conv_est, ylim=c(-.3, .1), pch=19, main= "Effect of HH Member Conviction on Voting (Varying Windows)", xlab="Weeks Around Election", ylab="Change in 2012 turnout (percentage points)")
abline(h=0, lty=2, col=gray(.4))
segments(storage$row, storage$conv_lowCI, storage$row, storage$conv_highCI, col="dodgerblue4")
dev.off()

pdf("beforeafter_jail_windows40_covars.pdf")
plot(storage$row, storage$jail_est, ylim=c(-.3, .1), pch=19, main= "Effect of HH Member Jail Sentence on Voting (Varying Windows)", xlab="Weeks Around Election", ylab="Change in 2012 turnout (percentage points)")
abline(h=0, lty=2, col=gray(.4))
segments(storage$row, storage$jail_lowCI, storage$row, storage$jail_highCI, col="dodgerblue4")
dev.off()


#also, present this a little differently (more like an RDD)

checkweeks <- c(-40:40)
storage <- as.data.frame(matrix(NA, nrow=length(checkweeks), ncol=2))
for (i in 1:nrow(storage)){
	week <- checkweeks[i]
	lower <- ed2012 + week*7; higher <- lower+7 #add/subtract off days to get the window
	window <- smallneighborsfull2[firstcase < higher & firstcase >= lower & anyjail==1, ]
	#now just take some pretreatment characteristic means for this week.
	storage[i,1] <- week
	storage[i,2] <- mean(window$vote2012)
}

colnames(storage) <- c("week", "vote2012")
storage$row <- 1:nrow(storage)
pdf("turnout2012_jailed_loess_40weeks.pdf")
plot(storage$week, storage$vote2012, main= "2012 Turnout by Household Members", ylim=c(.25,.65), xlab="Case Timing (Resulting in Jail): Weeks From Election", ylab="Proportion of Registrants Voting, Nov. 2012", cex.main=1.5, cex.axis=1.5,cex=1.5, cex.lab=1.45)
abline(v=0, lty=2)
vote.lo <- loess(storage$vote2012 ~ storage$week)
j <- order(storage$week)
lines(storage$week[j],vote.lo$fitted[j],col="darkgray",lty=2)
#fit two separate smoothers
vote.lo1 <- loess(storage$vote2012[1:40] ~ storage$week[1:40])
j <- order(storage$week)
lines(storage$week[j],vote.lo1$fitted[j],col="red",lwd=3)
vote.lo2 <- loess(storage$vote2012[41:81] ~ storage$week[41:81])
j <- order(storage$week)[41:81]
lines(storage$week[41:81],vote.lo2$fitted,col="red",lwd=3)
dev.off() #Figure SI19


### Rerun the main paper table/figs with logit instead of OLS. 
around1m <-  smallneighborsfull2[smallneighborsfull2$firstcase <= "2012-12-06" & smallneighborsfull2$firstcase > "2012-10-06", ]; dim(around1m)
around1m[,chargebefore:=0]; around1m[firstcase < "2012-11-06",chargebefore:=1] #set up some treatment-timing indicators
around1m[,casebefore:=0]; around1m[firstcase < "2012-11-06",casebefore:=1] 
around1m[anyconv==0,treatment:="chargeonly"] 
around1m[anyconv==1 & anyjail==0,treatment:="convonly"] 
around1m[anyjail==1,treatment:="jail"] 
around1m$treatfac <- as.factor(around1m$treatment)

with(around1m, tapply(vote2012, list(casebefore, treatfac), mean))

library(clusterSEs)
ba3l <- glm(vote2012 ~ casebefore + treatfac + casebefore*treatfac, family=binomial(link='logit'), data=around1m); summary(ba3l)
ba3lclust <- cluster.bs.glm(ba3l, around1m, cluster= ~def_spn, ci.level=.95, ) #why does this not return a vcov matrix or SEs? 

ba3cl <- glm(vote2012 ~ casebefore + treatfac + casebefore*treatfac+voter_male+voter_age + male + black + vote2006+ vote2008 +vote2010, family=binomial(link='logit'), data=around1m); summary(ba3cl)
ba3clclust <- cluster.bs.glm(ba3cl, around1m, cluster= ~def_spn, ci.level=.95, )

#Table SI.2
stargazer(ba3l, ba3cl,label= "beforeafter_1m_factor_logit",order=c(1, 2, 3, 11, 12, 4, 5, 6, 7, 8, 9, 10,13),align=T, omit.stat=c("LL","ser","f"), title="Proximal Contact on Voting (Using Case Timing): Logit", covariate.labels=c("Case Before Election", "HH Member Convicted", "HH Member Jailed", "HH Member Convicted * Before Election", "HH Member Jailed * Before Election","Voter Male", "Voter Age (Years)", "HH Member Male", "HH Member Black", "2006 Turnout", "2008 Turnout", "2010 Turnout" ),
dep.var.labels= c("Voted 2012"),
ci=TRUE, ci.custom= list(ba3lclust$ci, ba3clclust$ci),  #swap in new CI's and p-vals
p = list(ba3lclust$p.values, ba3clclust$p.values),
out="beforeafter_1m_factorsetup_logit.tex",star.cutoffs=c(0.05), notes="$^{*}$p$<$0.05", notes.append=FALSE)

#also, for the text: do some sort of predicted probability/first diffs based on model 1, to give some intuition about effect size? (see below--pretty similar to main table 1, little smaller.)
person1 <- data.frame(casebefore = 0, treatfac="jail")
person2 <- data.frame(casebefore = 1, treatfac="jail")
predict(ba3l, person1, type="response") - predict(ba3l, person2, type="response") #about a 9-pt diff for jail before vs. after

person1a <- data.frame(casebefore = 0, treatfac="convonly")
person2a <- data.frame(casebefore = 1, treatfac="convonly")
predict(ba3l, person1a, type="response") - predict(ba3l, person2a, type="response") #about a 14-pt diff for conviction

#do this every week from 1 week out to a year or so, for all three of these outcomes (just charge, just conv, jail)
#just plotting logit coeffs.
ed2012 <- as.Date("2012-11-06") #election day

smallneighborsfull2[,chargebefore:=0]; smallneighborsfull2[firstcase < "2012-11-06" & anyconv==0,chargebefore:=1] 
smallneighborsfull2[,convbefore:=0]; smallneighborsfull2[firstcase < "2012-11-06" & anyconv==1 & anyjail==0,convbefore:=1] 
smallneighborsfull2[,jailbefore:=0]; smallneighborsfull2[firstcase < "2012-11-06" & anyconv==1 & anyjail==1,jailbefore:=1] 

weeks <- 1*40
storage <- as.data.frame(matrix(NA, nrow=weeks, ncol=13))
for (i in 2:weeks){ #apologies; this is going to be slow, but I only needed to run it once so didn't optimize. 
	lower <- ed2012 - i*7; higher <- ed2012 + i*7 
	window <- smallneighborsfull2[firstcase < higher & firstcase > lower, ]

	ba1 <- glm(vote2012 ~ chargebefore, family=binomial(link='logit'), data=window[anyconv==0]); summary(ba1) #est. just-charge model (logit)
	ba1.clust<-cluster.bs.glm(ba1, window[anyconv==0], cluster= ~def_spn, ci.level=.95) #cluster SE's

	ba1conv <- glm(vote2012 ~ convbefore, family=binomial(link='logit'), data=window[anyconv==1 & anyjail==0,]); summary(ba1) #just-conv
	ba1conv.clust<-cluster.bs.glm(ba1conv, window[anyconv==1& anyjail==0,], cluster= ~def_spn, ci.level=.95)

	ba1jail <- glm(vote2012 ~ jailbefore, family=binomial(link='logit'), data=window[anyjail==1,]); summary(ba1) #jail
	ba1jail.clust<-cluster.bs.glm(ba1jail, window[anyjail==1,], cluster= ~def_spn, ci.level=.95)

	storage[i,1] <- nrow(window) 
	storage[i,2] <- est <- ba1$coefficients[2]
	storage[i,3] <- p <- ba1.clust$p.values[2]
	storage[i,4] <- ba1.clust$ci[2]
	storage[i,5] <- ba1.clust$ci[4]

	storage[i,6] <- est <- ba1conv$coefficients[2]
	storage[i,7] <- p <- ba1conv.clust$p.values[2]
	storage[i,8] <- ba1conv.clust$ci[2]
	storage[i,9] <- ba1conv.clust$ci[4]

	storage[i,10] <- est <- ba1jail$coefficients[2]
	storage[i,11] <- p <- ba1conv.clust$p.values[2]
	storage[i,12] <- ba1jail.clust$ci[2]
	storage[i,13] <- ba1conv.clust$ci[4]
}
colnames(storage) <- c("n", "case_est", "case_p", "case_lowCI", "case_highCI", "conv_est", "conv_p", "conv_lowCI", "conv_highCI", "jail_est", "jail_p", "jail_lowCI", "jail_highCI")
storage$row <- 1:nrow(storage)

#Figure SI.2
pdf("beforeafter_justcharge_windows40_logit.pdf")
plot(storage$row, storage$case_est, ylim=c(-.7, .3), pch=19,main= "HH Member Misdemeanor Charge (Logit version)", xlab="Weeks Around Election", ylab="Logit coefficient")
abline(h=0, lty=2, col=gray(.4))
segments(storage$row, storage$case_lowCI, storage$row, storage$case_highCI, col="dodgerblue4")
dev.off()

pdf("beforeafter_justconv_windows40_logit.pdf")
plot(storage$row, storage$conv_est, ylim=c(-.7, .3), pch=19, main= "HH Member Conviction (Logit version)", xlab="Weeks Around Election", ylab="Logit coefficient")
abline(h=0, lty=2, col=gray(.4))
segments(storage$row, storage$conv_lowCI, storage$row, storage$conv_highCI, col="dodgerblue4")
dev.off()

pdf("beforeafter_jail_windows40_logit.pdf")
plot(storage$row, storage$jail_est, ylim=c(-.7, .3), pch=19, main= "HH Member Jail Sentence (Logit version)", xlab="Weeks Around Election", ylab="Logit coefficient")
abline(h=0, lty=2, col=gray(.4))
segments(storage$row, storage$jail_lowCI, storage$row, storage$jail_highCI, col="dodgerblue4")
dev.off()

#and do quick predicted probs for the last week's model
person1 <- data.frame(convbefore = 0); person2 <- data.frame(convbefore = 1)
predict(ba1conv, person1, type="response") - predict(ba1conv, person2, type="response") #right, so that's actually like a 2-pp. difference. 


#########################
##and now redo the main conviction figure, but with slices of time rather than windows.

weeks <- 1*20
storage <- as.data.frame(matrix(NA, nrow=weeks, ncol=13))
for (i in 1:weeks){
	lower <- ed2012 - i*14; higher <- ed2012 + i*14 #add/subtract off days to get the window
	window <- smallneighborsfull2[(firstcase < higher & firstcase > higher-14) | (firstcase > lower & firstcase < lower+14), ]
	ba1 <- lm(vote2012 ~ chargebefore, data=window[anyconv==0]); summary(ba1) #est. just-charge model
	ba1.vcovCL<-cluster.vcov(ba1, window[anyconv==0]$def_spn)
	ba1conv <- lm(vote2012 ~ convbefore, data=window[anyconv==1 & anyjail==0,]); summary(ba1) #just-conv
	ba1conv.vcovCL<-cluster.vcov(ba1conv, window[anyconv==1& anyjail==0,]$def_spn)
	ba1jail <- lm(vote2012 ~ jailbefore, data=window[anyjail==1,]); summary(ba1) #jail
	ba1jail.vcovCL<-cluster.vcov(ba1jail, window[anyjail==1,]$def_spn)

	storage[i,1] <- nrow(window) 
	storage[i,2] <- est <- coeftest(ba1, ba1.vcovCL)[2,1]
	storage[i,3] <- p <- coeftest(ba1, ba1.vcovCL)[2,4]
	err<- coeftest(ba1, ba1.vcovCL)[2,2]; dof <- ba1$df
	storage[i,4] <- est + -1*err*qt(0.975, dof) 
	storage[i,5] <- est + 1*err*qt(0.975, dof) 

	storage[i,6] <- est <- coeftest(ba1conv, ba1conv.vcovCL)[2,1]
	storage[i,7] <- p <- coeftest(ba1conv, ba1conv.vcovCL)[2,4]
	err<- coeftest(ba1conv, ba1conv.vcovCL)[2,2]; dof <- ba1conv$df
	storage[i,8] <- est + -1*err*qt(0.975, dof) 
	storage[i,9] <- est + 1*err*qt(0.975, dof) 

	storage[i,10] <- est <- coeftest(ba1jail, ba1jail.vcovCL)[2,1]
	storage[i,11] <- p <- coeftest(ba1jail, ba1jail.vcovCL)[2,4]
	err<- coeftest(ba1jail, ba1jail.vcovCL)[2,2]; dof <- ba1jail$df
	storage[i,12] <- est + -1*err*qt(0.975, dof) 
	storage[i,13] <- est + 1*err*qt(0.975, dof) 
}
colnames(storage) <- c("n", "case_est", "case_p", "case_lowCI", "case_highCI", "conv_est", "conv_p", "conv_lowCI", "conv_highCI", "jail_est", "jail_p", "jail_lowCI", "jail_highCI")
storage$row <- (1:nrow(storage))*2

#Figure SI9
pdf("beforeafter_justconv_slices40.pdf")
plot(storage$row, storage$conv_est, pch=19, main= "Effect of HH Member Conviction on Voting (2-week slices)", xlab="Weeks From Election", ylab="Change in 2012 turnout (percentage points)")
abline(h=0, lty=2, col=gray(.4))
segments(storage$row, storage$conv_lowCI, storage$row, storage$conv_highCI, col="dodgerblue4")
dev.off()


##now generate the same (original 3) plots for a couple of subgroups. ("robustness" and "effect heterogeneity" sections of the SI)
# start with "best matches": HHs where defendant, voter share a last name.
#variable "samename" created above. 
samename <- smallneighborsfull2[samename==1,];dim(samename)
weeks <- 1*40
storage <- as.data.frame(matrix(NA, nrow=weeks, ncol=13))
for (i in 2:weeks){
	lower <- ed2012 - i*7; higher <- ed2012 + i*7 #add/subtract off days to get the window
	window <- samename[firstcase < higher & firstcase > lower, ]
	ba1 <- lm(vote2012 ~ chargebefore, data=window[anyconv==0]); summary(ba1) #est. just-charge model
	ba1.vcovCL<-cluster.vcov(ba1, window[anyconv==0]$def_spn)
	ba1conv <- lm(vote2012 ~ convbefore, data=window[anyconv==1 & anyjail==0,]); summary(ba1) #just-conv
	ba1conv.vcovCL<-cluster.vcov(ba1conv, window[anyconv==1& anyjail==0,]$def_spn)
	ba1jail <- lm(vote2012 ~ jailbefore, data=window[anyjail==1,]); summary(ba1) #jail
	ba1jail.vcovCL<-cluster.vcov(ba1jail, window[anyjail==1,]$def_spn)

	storage[i,1] <- nrow(window) 
	storage[i,2] <- est <- coeftest(ba1, ba1.vcovCL)[2,1]
	storage[i,3] <- p <- coeftest(ba1, ba1.vcovCL)[2,4]
	err<- coeftest(ba1, ba1.vcovCL)[2,2]; dof <- ba1$df
	storage[i,4] <- est + -1*err*qt(0.975, dof) 
	storage[i,5] <- est + 1*err*qt(0.975, dof) 

	storage[i,6] <- est <- coeftest(ba1conv, ba1conv.vcovCL)[2,1]
	storage[i,7] <- p <- coeftest(ba1conv, ba1conv.vcovCL)[2,4]
	err<- coeftest(ba1conv, ba1conv.vcovCL)[2,2]; dof <- ba1conv$df
	storage[i,8] <- est + -1*err*qt(0.975, dof) 
	storage[i,9] <- est + 1*err*qt(0.975, dof) 

	storage[i,10] <- est <- coeftest(ba1jail, ba1jail.vcovCL)[2,1]
	storage[i,11] <- p <- coeftest(ba1jail, ba1jail.vcovCL)[2,4]
	err<- coeftest(ba1jail, ba1jail.vcovCL)[2,2]; dof <- ba1jail$df
	storage[i,12] <- est + -1*err*qt(0.975, dof) 
	storage[i,13] <- est + 1*err*qt(0.975, dof) 
}
colnames(storage) <- c("n", "case_est", "case_p", "case_lowCI", "case_highCI", "conv_est", "conv_p", "conv_lowCI", "conv_highCI", "jail_est", "jail_p", "jail_lowCI", "jail_highCI")
storage$row <- 1:nrow(storage)

#Figure SI3
pdf("beforeafter_justcharge_windows40_lastnamematch.pdf")
plot(storage$row, storage$case_est, ylim=c(-.3, .1), pch=19,main= "Effect of HH Member Misdemeanor Charge on Voting (Varying Windows)", xlab="Weeks Around Election", ylab="Change in 2012 turnout (percentage points)")
abline(h=0, lty=2, col=gray(.4))
segments(storage$row, storage$case_lowCI, storage$row, storage$case_highCI, col="dodgerblue4")
dev.off()

pdf("beforeafter_justconv_windows40_lastnamematch.pdf")
plot(storage$row, storage$conv_est, ylim=c(-.3, .1), pch=19, main= "Effect of HH Member Conviction on Voting (Varying Windows)", xlab="Weeks Around Election", ylab="Change in 2012 turnout (percentage points)")
abline(h=0, lty=2, col=gray(.4))
segments(storage$row, storage$conv_lowCI, storage$row, storage$conv_highCI, col="dodgerblue4")
dev.off()

pdf("beforeafter_jail_windows40_lastnamematch.pdf")
plot(storage$row, storage$jail_est, ylim=c(-.3, .1), pch=19, main= "Effect of HH Member Jail Sentence on Voting (Varying Windows)", xlab="Weeks Around Election", ylab="Change in 2012 turnout (percentage points)")
abline(h=0, lty=2, col=gray(.4))
segments(storage$row, storage$jail_lowCI, storage$row, storage$jail_highCI, col="dodgerblue4")
dev.off()

## and now make sure we drop people who might have been "treated" before the election by a felony case, even if they didn't see misdemeanor cases until afterwards
nofelons <- smallneighborsfull2[felondrop==0,]

weeks <- 1*40
storage <- as.data.frame(matrix(NA, nrow=weeks, ncol=13))
for (i in 2:weeks){
	lower <- ed2012 - i*7; higher <- ed2012 + i*7 #add/subtract off days to get the window
	window <- nofelons[firstcase < higher & firstcase > lower, ]
	ba1 <- lm(vote2012 ~ chargebefore+vote2010+vote2008+vote2006+vote2004, data=window[anyconv==0]); summary(ba1) #est. just-charge model
	ba1.vcovCL<-cluster.vcov(ba1, window[anyconv==0]$def_spn)
	ba1conv <- lm(vote2012 ~ convbefore+vote2010+vote2008+vote2006+vote2004, data=window[anyconv==1 & anyjail==0,]); summary(ba1) #just-conv
	ba1conv.vcovCL<-cluster.vcov(ba1conv, window[anyconv==1& anyjail==0,]$def_spn)
	ba1jail <- lm(vote2012 ~ jailbefore+vote2010+vote2008+vote2006+vote2004, data=window[anyjail==1,]); summary(ba1) #jail
	ba1jail.vcovCL<-cluster.vcov(ba1jail, window[anyjail==1,]$def_spn)

	storage[i,1] <- nrow(window) 
	storage[i,2] <- est <- coeftest(ba1, ba1.vcovCL)[2,1]
	storage[i,3] <- p <- coeftest(ba1, ba1.vcovCL)[2,4]
	err<- coeftest(ba1, ba1.vcovCL)[2,2]; dof <- ba1$df
	storage[i,4] <- est + -1*err*qt(0.975, dof) 
	storage[i,5] <- est + 1*err*qt(0.975, dof) 

	storage[i,6] <- est <- coeftest(ba1conv, ba1conv.vcovCL)[2,1]
	storage[i,7] <- p <- coeftest(ba1conv, ba1conv.vcovCL)[2,4]
	err<- coeftest(ba1conv, ba1conv.vcovCL)[2,2]; dof <- ba1conv$df
	storage[i,8] <- est + -1*err*qt(0.975, dof) 
	storage[i,9] <- est + 1*err*qt(0.975, dof) 

	storage[i,10] <- est <- coeftest(ba1jail, ba1jail.vcovCL)[2,1]
	storage[i,11] <- p <- coeftest(ba1jail, ba1jail.vcovCL)[2,4]
	err<- coeftest(ba1jail, ba1jail.vcovCL)[2,2]; dof <- ba1jail$df
	storage[i,12] <- est + -1*err*qt(0.975, dof) 
	storage[i,13] <- est + 1*err*qt(0.975, dof) 
}
colnames(storage) <- c("n", "case_est", "case_p", "case_lowCI", "case_highCI", "conv_est", "conv_p", "conv_lowCI", "conv_highCI", "jail_est", "jail_p", "jail_lowCI", "jail_highCI")
storage$row <- 1:nrow(storage)

#Figure SI6
pdf("beforeafter_justcharge_windows40_felonydrop.pdf")
par(mfrow=c(1,3))
plot(storage$row, storage$case_est, ylim=c(-.3, .1), pch=19,main= "Charge", xlab="Weeks Around Election", ylab="Change in 2012 turnout (percentage points)")
abline(h=0, lty=2, col=gray(.4))
segments(storage$row, storage$case_lowCI, storage$row, storage$case_highCI, col="dodgerblue4")
#dev.off()
#pdf("beforeafter_justconv_windows40_lastnamematch.pdf")
plot(storage$row, storage$conv_est, ylim=c(-.3, .1), pch=19, main= "Conviction", xlab="Weeks Around Election", ylab="Change in 2012 turnout (percentage points)")
abline(h=0, lty=2, col=gray(.4))
segments(storage$row, storage$conv_lowCI, storage$row, storage$conv_highCI, col="dodgerblue4")
#dev.off()
#pdf("beforeafter_jail_windows40_lastnamematch.pdf")
plot(storage$row, storage$jail_est, ylim=c(-.3, .1), pch=19, main= "Jail", xlab="Weeks Around Election", ylab="Change in 2012 turnout (percentage points)")
abline(h=0, lty=2, col=gray(.4))
segments(storage$row, storage$jail_lowCI, storage$row, storage$jail_highCI, col="dodgerblue4")
dev.off()



## and now same deal, but only for voters matched to Black defendants.
black <- smallneighborsfull2[black==1,] 
dim(black); dim(smallneighborsfull2)
weeks <- 1*40
storage <- as.data.frame(matrix(NA, nrow=weeks, ncol=13))
for (i in 2:weeks){
	lower <- ed2012 - i*7; higher <- ed2012 + i*7 #add/subtract off days to get the window
	window <- black[firstcase < higher & firstcase > lower, ]
	
	ba1 <- lm(vote2012 ~ chargebefore, data=window[anyconv==0]); summary(ba1) #est. just-charge model
	ba1.vcovCL<-cluster.vcov(ba1, window[anyconv==0]$def_spn)
	ba1conv <- lm(vote2012 ~ convbefore, data=window[anyconv==1 & anyjail==0,]); summary(ba1) #just-conv
	ba1conv.vcovCL<-cluster.vcov(ba1conv, window[anyconv==1& anyjail==0,]$def_spn)
	ba1jail <- lm(vote2012 ~ jailbefore, data=window[anyjail==1,]); summary(ba1) #jail
	ba1jail.vcovCL<-cluster.vcov(ba1jail, window[anyjail==1,]$def_spn)

	storage[i,1] <- nrow(window) 
	storage[i,2] <- est <- coeftest(ba1, ba1.vcovCL)[2,1]
	storage[i,3] <- p <- coeftest(ba1, ba1.vcovCL)[2,4]
	err<- coeftest(ba1, ba1.vcovCL)[2,2]; dof <- ba1$df
	storage[i,4] <- est + -1*err*qt(0.975, dof) 
	storage[i,5] <- est + 1*err*qt(0.975, dof) 

	storage[i,6] <- est <- coeftest(ba1conv, ba1conv.vcovCL)[2,1]
	storage[i,7] <- p <- coeftest(ba1conv, ba1conv.vcovCL)[2,4]
	err<- coeftest(ba1conv, ba1conv.vcovCL)[2,2]; dof <- ba1conv$df
	storage[i,8] <- est + -1*err*qt(0.975, dof) 
	storage[i,9] <- est + 1*err*qt(0.975, dof) 

	storage[i,10] <- est <- coeftest(ba1jail, ba1jail.vcovCL)[2,1]
	storage[i,11] <- p <- coeftest(ba1jail, ba1jail.vcovCL)[2,4]
	err<- coeftest(ba1jail, ba1jail.vcovCL)[2,2]; dof <- ba1jail$df
	storage[i,12] <- est + -1*err*qt(0.975, dof) 
	storage[i,13] <- est + 1*err*qt(0.975, dof) 
}
colnames(storage) <- c("n", "case_est", "case_p", "case_lowCI", "case_highCI", "conv_est", "conv_p", "conv_lowCI", "conv_highCI", "jail_est", "jail_p", "jail_lowCI", "jail_highCI")
storage$row <- 1:nrow(storage)

#Figure SI10 
pdf("beforeafter_justcharge_windows40_lastnamematch_black.pdf") #note that this naming isn't right-- this isn't restricted to last name matches.
plot(storage$row, storage$case_est, ylim=c(-.3, .1), pch=19,main= "Effect of HH Member Misdemeanor Charge on Voting (Varying Windows)", xlab="Weeks Around Election", ylab="Change in 2012 turnout (percentage points)")
abline(h=0, lty=2, col=gray(.4))
segments(storage$row, storage$case_lowCI, storage$row, storage$case_highCI, col="dodgerblue4")
dev.off()

pdf("beforeafter_justconv_windows40_lastnamematch_black.pdf")
plot(storage$row, storage$conv_est, ylim=c(-.3, .1), pch=19, main= "Effect of HH Member Conviction on Voting (Varying Windows)", xlab="Weeks Around Election", ylab="Change in 2012 turnout (percentage points)")
abline(h=0, lty=2, col=gray(.4))
segments(storage$row, storage$conv_lowCI, storage$row, storage$conv_highCI, col="dodgerblue4")
dev.off()

pdf("beforeafter_jail_windows40_lastnamematch_black.pdf")
plot(storage$row, storage$jail_est, ylim=c(-.3, .1), pch=19, main= "Effect of HH Member Jail Sentence on Voting (Varying Windows)", xlab="Weeks Around Election", ylab="Change in 2012 turnout (percentage points)")
abline(h=0, lty=2, col=gray(.4))
segments(storage$row, storage$jail_lowCI, storage$row, storage$jail_highCI, col="dodgerblue4")
dev.off()


#### and run it for people who did/didn't vote in 2008.
### (Figures SI13 and SI14)
table(smallneighborsfull2$vote2008)
voted08 <- smallneighborsfull2[vote2008==1,]
nonvoted08 <- smallneighborsfull2[vote2008==0,]

weeks <- 1*40
storage <- as.data.frame(matrix(NA, nrow=weeks, ncol=13))
for (i in 2:weeks){
	lower <- ed2012 - i*7; higher <- ed2012 + i*7 #add/subtract off days to get the window
	window <- voted08[firstcase < higher & firstcase > lower, ]
	ba1 <- lm(vote2012 ~ chargebefore, data=window[anyconv==0]); summary(ba1) #est. just-charge model
	ba1.vcovCL<-cluster.vcov(ba1, window[anyconv==0]$def_spn)
	ba1conv <- lm(vote2012 ~ convbefore, data=window[anyconv==1 & anyjail==0,]); summary(ba1) #just-conv
	ba1conv.vcovCL<-cluster.vcov(ba1conv, window[anyconv==1& anyjail==0,]$def_spn)
	ba1jail <- lm(vote2012 ~ jailbefore, data=window[anyjail==1,]); summary(ba1) #jail
	ba1jail.vcovCL<-cluster.vcov(ba1jail, window[anyjail==1,]$def_spn)

	storage[i,1] <- nrow(window) 
	storage[i,2] <- est <- coeftest(ba1, ba1.vcovCL)[2,1]
	storage[i,3] <- p <- coeftest(ba1, ba1.vcovCL)[2,4]
	err<- coeftest(ba1, ba1.vcovCL)[2,2]; dof <- ba1$df
	storage[i,4] <- est + -1*err*qt(0.975, dof) 
	storage[i,5] <- est + 1*err*qt(0.975, dof) 

	storage[i,6] <- est <- coeftest(ba1conv, ba1conv.vcovCL)[2,1]
	storage[i,7] <- p <- coeftest(ba1conv, ba1conv.vcovCL)[2,4]
	err<- coeftest(ba1conv, ba1conv.vcovCL)[2,2]; dof <- ba1conv$df
	storage[i,8] <- est + -1*err*qt(0.975, dof) 
	storage[i,9] <- est + 1*err*qt(0.975, dof) 

	storage[i,10] <- est <- coeftest(ba1jail, ba1jail.vcovCL)[2,1]
	storage[i,11] <- p <- coeftest(ba1jail, ba1jail.vcovCL)[2,4]
	err<- coeftest(ba1jail, ba1jail.vcovCL)[2,2]; dof <- ba1jail$df
	storage[i,12] <- est + -1*err*qt(0.975, dof) 
	storage[i,13] <- est + 1*err*qt(0.975, dof) 
}
colnames(storage) <- c("n", "case_est", "case_p", "case_lowCI", "case_highCI", "conv_est", "conv_p", "conv_lowCI", "conv_highCI", "jail_est", "jail_p", "jail_lowCI", "jail_highCI")
storage$row <- 1:nrow(storage)


pdf("beforeafter_3panel_windows40_voted08.pdf") #stick all in one panel for now.
par(mfrow=c(3,1))

#pdf("beforeafter_justcharge_windows40_voted08.pdf")
plot(storage$row, storage$case_est, ylim=c(-.3, .1), pch=19,main= "Effect of HH Member Misdemeanor Charge on Voting (Varying Windows)", xlab="Weeks Around Election", ylab="Change in 2012 turnout (percentage points)")
abline(h=0, lty=2, col=gray(.4))
segments(storage$row, storage$case_lowCI, storage$row, storage$case_highCI, col="dodgerblue4")
#dev.off()

#pdf("beforeafter_justconv_windows40_voted08.pdf")
plot(storage$row, storage$conv_est, ylim=c(-.3, .1), pch=19, main= "Effect of HH Member Conviction on Voting (Varying Windows)", xlab="Weeks Around Election", ylab="Change in 2012 turnout (percentage points)")
abline(h=0, lty=2, col=gray(.4))
segments(storage$row, storage$conv_lowCI, storage$row, storage$conv_highCI, col="dodgerblue4")
#dev.off()

#pdf("beforeafter_jail_windows40_voted08.pdf")
plot(storage$row, storage$jail_est, ylim=c(-.3, .1), pch=19, main= "Effect of HH Member Jail Sentence on Voting (Varying Windows)", xlab="Weeks Around Election", ylab="Change in 2012 turnout (percentage points)")
abline(h=0, lty=2, col=gray(.4))
segments(storage$row, storage$jail_lowCI, storage$row, storage$jail_highCI, col="dodgerblue4")

dev.off()



weeks <- 1*40
storage <- as.data.frame(matrix(NA, nrow=weeks, ncol=13))
for (i in 2:weeks){
	lower <- ed2012 - i*7; higher <- ed2012 + i*7 #add/subtract off days to get the window
	window <- nonvoted08[firstcase < higher & firstcase > lower, ]
	ba1 <- lm(vote2012 ~ chargebefore, data=window[anyconv==0]); summary(ba1) #est. just-charge model
	ba1.vcovCL<-cluster.vcov(ba1, window[anyconv==0]$def_spn)
	ba1conv <- lm(vote2012 ~ convbefore, data=window[anyconv==1 & anyjail==0,]); summary(ba1) #just-conv
	ba1conv.vcovCL<-cluster.vcov(ba1conv, window[anyconv==1& anyjail==0,]$def_spn)
	ba1jail <- lm(vote2012 ~ jailbefore, data=window[anyjail==1,]); summary(ba1) #jail
	ba1jail.vcovCL<-cluster.vcov(ba1jail, window[anyjail==1,]$def_spn)

	storage[i,1] <- nrow(window) 
	storage[i,2] <- est <- coeftest(ba1, ba1.vcovCL)[2,1]
	storage[i,3] <- p <- coeftest(ba1, ba1.vcovCL)[2,4]
	err<- coeftest(ba1, ba1.vcovCL)[2,2]; dof <- ba1$df
	storage[i,4] <- est + -1*err*qt(0.975, dof) 
	storage[i,5] <- est + 1*err*qt(0.975, dof) 

	storage[i,6] <- est <- coeftest(ba1conv, ba1conv.vcovCL)[2,1]
	storage[i,7] <- p <- coeftest(ba1conv, ba1conv.vcovCL)[2,4]
	err<- coeftest(ba1conv, ba1conv.vcovCL)[2,2]; dof <- ba1conv$df
	storage[i,8] <- est + -1*err*qt(0.975, dof) 
	storage[i,9] <- est + 1*err*qt(0.975, dof) 

	storage[i,10] <- est <- coeftest(ba1jail, ba1jail.vcovCL)[2,1]
	storage[i,11] <- p <- coeftest(ba1jail, ba1jail.vcovCL)[2,4]
	err<- coeftest(ba1jail, ba1jail.vcovCL)[2,2]; dof <- ba1jail$df
	storage[i,12] <- est + -1*err*qt(0.975, dof) 
	storage[i,13] <- est + 1*err*qt(0.975, dof) 
}
colnames(storage) <- c("n", "case_est", "case_p", "case_lowCI", "case_highCI", "conv_est", "conv_p", "conv_lowCI", "conv_highCI", "jail_est", "jail_p", "jail_lowCI", "jail_highCI")
storage$row <- 1:nrow(storage)


pdf("beforeafter_3panel_windows40_nonvoted08.pdf") #stick all in one panel for now.
par(mfrow=c(3,1))
#pdf("beforeafter_justcharge_windows40_voted08.pdf")
plot(storage$row, storage$case_est, ylim=c(-.3, .1), pch=19,main= "Effect of HH Member Misdemeanor Charge on Voting (Varying Windows)", xlab="Weeks Around Election", ylab="Change in 2012 turnout (percentage points)")
abline(h=0, lty=2, col=gray(.4))
segments(storage$row, storage$case_lowCI, storage$row, storage$case_highCI, col="dodgerblue4")
#dev.off()

#pdf("beforeafter_justconv_windows40_voted08.pdf")
plot(storage$row, storage$conv_est, ylim=c(-.3, .1), pch=19, main= "Effect of HH Member Conviction on Voting (Varying Windows)", xlab="Weeks Around Election", ylab="Change in 2012 turnout (percentage points)")
abline(h=0, lty=2, col=gray(.4))
segments(storage$row, storage$conv_lowCI, storage$row, storage$conv_highCI, col="dodgerblue4")
#dev.off()

#pdf("beforeafter_jail_windows40_voted08.pdf")
plot(storage$row, storage$jail_est, ylim=c(-.3, .1), pch=19, main= "Effect of HH Member Jail Sentence on Voting (Varying Windows)", xlab="Weeks Around Election", ylab="Change in 2012 turnout (percentage points)")
abline(h=0, lty=2, col=gray(.4))
segments(storage$row, storage$jail_lowCI, storage$row, storage$jail_highCI, col="dodgerblue4")

dev.off()


#### and now look for family relationships
### (Figures SI17 and SI18)
smallneighborsfull2[, oppsex:=0] 
smallneighborsfull2[male==T & voter_male==F, oppsex:=1] #first, gen var for whether voter and hh member are of opposite sexes (yes, heteronormative but will get many partners)
smallneighborsfull2[male==F & voter_male==T, oppsex:=1] 

#then check whether they're within 15 yrs of age (this is now calculated in the setup file) 
smallneighborsfull2[, partners15ss:= 0]
smallneighborsfull2[oppsex == 1 & agediff <=15, partners15ss:= 1]

#oh, and also try to identify parents/older relatives (for this, just go with people 15+ years older than the defendant)
smallneighborsfull2[, parents15:= 0]

smallneighborsfull2[agediff1 >=15, parents15:= 1]

smallneighborsfull2[, parents18:= 0]
smallneighborsfull2[agediff1 >=18, parents18:= 1] #stricter cutoff: will catch fewer roommates/partners but will miss some young parents.

#and can we find kids/younger relatives of voter?
smallneighborsfull2[, kids15:= 0]
smallneighborsfull2[agediff1 <=-15, kids15:= 1]

parents <- smallneighborsfull2[parents15==1,]; dim(parents)
partners <- smallneighborsfull2[partners15ss==1,]; dim(partners)


weeks <- 1*40
storage <- as.data.frame(matrix(NA, nrow=weeks, ncol=13))
for (i in 2:weeks){
	lower <- ed2012 - i*7; higher <- ed2012 + i*7 #add/subtract off days to get the window
	window <- parents[firstcase < higher & firstcase > lower, ]
	ba1 <- lm(vote2012 ~ chargebefore, data=window[anyconv==0]); summary(ba1) #est. just-charge model
	ba1.vcovCL<-cluster.vcov(ba1, window[anyconv==0]$def_spn)
	ba1conv <- lm(vote2012 ~ convbefore, data=window[anyconv==1 & anyjail==0,]); summary(ba1) #just-conv
	ba1conv.vcovCL<-cluster.vcov(ba1conv, window[anyconv==1& anyjail==0,]$def_spn)
	ba1jail <- lm(vote2012 ~ jailbefore, data=window[anyjail==1,]); summary(ba1) #jail
	ba1jail.vcovCL<-cluster.vcov(ba1jail, window[anyjail==1,]$def_spn)

	storage[i,1] <- nrow(window) 
	storage[i,2] <- est <- coeftest(ba1, ba1.vcovCL)[2,1]
	storage[i,3] <- p <- coeftest(ba1, ba1.vcovCL)[2,4]
	err<- coeftest(ba1, ba1.vcovCL)[2,2]; dof <- ba1$df
	storage[i,4] <- est + -1*err*qt(0.975, dof) 
	storage[i,5] <- est + 1*err*qt(0.975, dof) 

	storage[i,6] <- est <- coeftest(ba1conv, ba1conv.vcovCL)[2,1]
	storage[i,7] <- p <- coeftest(ba1conv, ba1conv.vcovCL)[2,4]
	err<- coeftest(ba1conv, ba1conv.vcovCL)[2,2]; dof <- ba1conv$df
	storage[i,8] <- est + -1*err*qt(0.975, dof) 
	storage[i,9] <- est + 1*err*qt(0.975, dof) 

	storage[i,10] <- est <- coeftest(ba1jail, ba1jail.vcovCL)[2,1]
	storage[i,11] <- p <- coeftest(ba1jail, ba1jail.vcovCL)[2,4]
	err<- coeftest(ba1jail, ba1jail.vcovCL)[2,2]; dof <- ba1jail$df
	storage[i,12] <- est + -1*err*qt(0.975, dof) 
	storage[i,13] <- est + 1*err*qt(0.975, dof) 
}
colnames(storage) <- c("n", "case_est", "case_p", "case_lowCI", "case_highCI", "conv_est", "conv_p", "conv_lowCI", "conv_highCI", "jail_est", "jail_p", "jail_lowCI", "jail_highCI")
storage$row <- 1:nrow(storage)


pdf("beforeafter_3panel_windows40_parents.pdf") #stick all in one panel for now.
par(mfrow=c(3,1))

#pdf("beforeafter_justcharge_windows40_parents.pdf")
plot(storage$row, storage$case_est, ylim=c(-.3, .1), pch=19,main= "Effect of HH Member Misdemeanor Charge on Voting (Varying Windows)", xlab="Weeks Around Election", ylab="Change in 2012 turnout (percentage points)")
abline(h=0, lty=2, col=gray(.4))
segments(storage$row, storage$case_lowCI, storage$row, storage$case_highCI, col="dodgerblue4")
#dev.off()

#pdf("beforeafter_justconv_windows40_parents.pdf")
plot(storage$row, storage$conv_est, ylim=c(-.3, .1), pch=19, main= "Effect of HH Member Conviction on Voting (Varying Windows)", xlab="Weeks Around Election", ylab="Change in 2012 turnout (percentage points)")
abline(h=0, lty=2, col=gray(.4))
segments(storage$row, storage$conv_lowCI, storage$row, storage$conv_highCI, col="dodgerblue4")
#dev.off()

#pdf("beforeafter_jail_windows40_parents.pdf")
plot(storage$row, storage$jail_est, ylim=c(-.3, .1), pch=19, main= "Effect of HH Member Jail Sentence on Voting (Varying Windows)", xlab="Weeks Around Election", ylab="Change in 2012 turnout (percentage points)")
abline(h=0, lty=2, col=gray(.4))
segments(storage$row, storage$jail_lowCI, storage$row, storage$jail_highCI, col="dodgerblue4")

dev.off()



weeks <- 1*40
storage <- as.data.frame(matrix(NA, nrow=weeks, ncol=13))
for (i in 2:weeks){
	lower <- ed2012 - i*7; higher <- ed2012 + i*7 #add/subtract off days to get the window
	window <- partners[firstcase < higher & firstcase > lower, ]
	ba1 <- lm(vote2012 ~ chargebefore, data=window[anyconv==0]); summary(ba1) #est. just-charge model
	ba1.vcovCL<-cluster.vcov(ba1, window[anyconv==0]$def_spn)
	ba1conv <- lm(vote2012 ~ convbefore, data=window[anyconv==1 & anyjail==0,]); summary(ba1) #just-conv
	ba1conv.vcovCL<-cluster.vcov(ba1conv, window[anyconv==1& anyjail==0,]$def_spn)
	ba1jail <- lm(vote2012 ~ jailbefore, data=window[anyjail==1,]); summary(ba1) #jail
	ba1jail.vcovCL<-cluster.vcov(ba1jail, window[anyjail==1,]$def_spn)

	storage[i,1] <- nrow(window) 
	storage[i,2] <- est <- coeftest(ba1, ba1.vcovCL)[2,1]
	storage[i,3] <- p <- coeftest(ba1, ba1.vcovCL)[2,4]
	err<- coeftest(ba1, ba1.vcovCL)[2,2]; dof <- ba1$df
	storage[i,4] <- est + -1*err*qt(0.975, dof) 
	storage[i,5] <- est + 1*err*qt(0.975, dof) 

	storage[i,6] <- est <- coeftest(ba1conv, ba1conv.vcovCL)[2,1]
	storage[i,7] <- p <- coeftest(ba1conv, ba1conv.vcovCL)[2,4]
	err<- coeftest(ba1conv, ba1conv.vcovCL)[2,2]; dof <- ba1conv$df
	storage[i,8] <- est + -1*err*qt(0.975, dof) 
	storage[i,9] <- est + 1*err*qt(0.975, dof) 

	storage[i,10] <- est <- coeftest(ba1jail, ba1jail.vcovCL)[2,1]
	storage[i,11] <- p <- coeftest(ba1jail, ba1jail.vcovCL)[2,4]
	err<- coeftest(ba1jail, ba1jail.vcovCL)[2,2]; dof <- ba1jail$df
	storage[i,12] <- est + -1*err*qt(0.975, dof) 
	storage[i,13] <- est + 1*err*qt(0.975, dof) 
}
colnames(storage) <- c("n", "case_est", "case_p", "case_lowCI", "case_highCI", "conv_est", "conv_p", "conv_lowCI", "conv_highCI", "jail_est", "jail_p", "jail_lowCI", "jail_highCI")
storage$row <- 1:nrow(storage)


pdf("beforeafter_3panel_windows40_partners.pdf") #stick all in one panel for now.
par(mfrow=c(3,1))
#pdf("beforeafter_justcharge_windows40_partners.pdf")
plot(storage$row, storage$case_est, ylim=c(-.3, .1), pch=19,main= "Effect of HH Member Misdemeanor Charge on Voting (Varying Windows)", xlab="Weeks Around Election", ylab="Change in 2012 turnout (percentage points)")
abline(h=0, lty=2, col=gray(.4))
segments(storage$row, storage$case_lowCI, storage$row, storage$case_highCI, col="dodgerblue4")
#dev.off()

#pdf("beforeafter_justconv_windows40_partners.pdf")
plot(storage$row, storage$conv_est, ylim=c(-.3, .1), pch=19, main= "Effect of HH Member Conviction on Voting (Varying Windows)", xlab="Weeks Around Election", ylab="Change in 2012 turnout (percentage points)")
abline(h=0, lty=2, col=gray(.4))
segments(storage$row, storage$conv_lowCI, storage$row, storage$conv_highCI, col="dodgerblue4")
#dev.off()

#pdf("beforeafter_jail_windows40_partners.pdf")
plot(storage$row, storage$jail_est, ylim=c(-.3, .1), pch=19, main= "Effect of HH Member Jail Sentence on Voting (Varying Windows)", xlab="Weeks Around Election", ylab="Change in 2012 turnout (percentage points)")
abline(h=0, lty=2, col=gray(.4))
segments(storage$row, storage$jail_lowCI, storage$row, storage$jail_highCI, col="dodgerblue4")

dev.off()


#and now homeownership

homeowners <- smallneighborsfull2[homeowner==1,]
nonhomeowners <- smallneighborsfull2[homeowner==0,]

weeks <- 1*40
storage <- as.data.frame(matrix(NA, nrow=weeks, ncol=13))
for (i in 2:weeks){
	lower <- ed2012 - i*7; higher <- ed2012 + i*7 #add/subtract off days to get the window
	window <- homeowners[firstcase < higher & firstcase > lower, ]
	#window[,chargebefore:=0]; window[firstcase < "2012-11-06",chargebefore:=1] #set up some treatment-timing indicators (these are above now)
	#window <- window[!(firstcase > ed2012-3 & firstcase < ed2012+1), ] #drop the few days just prior to election, since that's hard to attribute treatment
	ba1 <- lm(vote2012 ~ chargebefore, data=window[anyconv==0]); summary(ba1) #est. just-charge model
	ba1.vcovCL<-cluster.vcov(ba1, window[anyconv==0]$def_spn)
	ba1conv <- lm(vote2012 ~ convbefore, data=window[anyconv==1 & anyjail==0,]); summary(ba1) #just-conv
	ba1conv.vcovCL<-cluster.vcov(ba1conv, window[anyconv==1& anyjail==0,]$def_spn)
	ba1jail <- lm(vote2012 ~ jailbefore, data=window[anyjail==1,]); summary(ba1) #jail
	ba1jail.vcovCL<-cluster.vcov(ba1jail, window[anyjail==1,]$def_spn)

	storage[i,1] <- nrow(window) 
	storage[i,2] <- est <- coeftest(ba1, ba1.vcovCL)[2,1]
	storage[i,3] <- p <- coeftest(ba1, ba1.vcovCL)[2,4]
	err<- coeftest(ba1, ba1.vcovCL)[2,2]; dof <- ba1$df
	storage[i,4] <- est + -1*err*qt(0.975, dof) 
	storage[i,5] <- est + 1*err*qt(0.975, dof) 

	storage[i,6] <- est <- coeftest(ba1conv, ba1conv.vcovCL)[2,1]
	storage[i,7] <- p <- coeftest(ba1conv, ba1conv.vcovCL)[2,4]
	err<- coeftest(ba1conv, ba1conv.vcovCL)[2,2]; dof <- ba1conv$df
	storage[i,8] <- est + -1*err*qt(0.975, dof) 
	storage[i,9] <- est + 1*err*qt(0.975, dof) 

	storage[i,10] <- est <- coeftest(ba1jail, ba1jail.vcovCL)[2,1]
	storage[i,11] <- p <- coeftest(ba1jail, ba1jail.vcovCL)[2,4]
	err<- coeftest(ba1jail, ba1jail.vcovCL)[2,2]; dof <- ba1jail$df
	storage[i,12] <- est + -1*err*qt(0.975, dof) 
	storage[i,13] <- est + 1*err*qt(0.975, dof) 
}
colnames(storage) <- c("n", "case_est", "case_p", "case_lowCI", "case_highCI", "conv_est", "conv_p", "conv_lowCI", "conv_highCI", "jail_est", "jail_p", "jail_lowCI", "jail_highCI")
storage$row <- 1:nrow(storage)

#Figure SI15
pdf("beforeafter_3panel_windows40_homeowner.pdf") #stick all in one panel for now.
par(mfrow=c(3,1))

#pdf("beforeafter_justcharge_windows40_homeowner.pdf")
plot(storage$row, storage$case_est, ylim=c(-.3, .1), pch=19,main= "Effect of HH Member Misdemeanor Charge on Voting (Varying Windows)", xlab="Weeks Around Election", ylab="Change in 2012 turnout (percentage points)")
abline(h=0, lty=2, col=gray(.4))
segments(storage$row, storage$case_lowCI, storage$row, storage$case_highCI, col="dodgerblue4")
#dev.off()

#pdf("beforeafter_justconv_windows40_homeowner.pdf")
plot(storage$row, storage$conv_est, ylim=c(-.3, .1), pch=19, main= "Effect of HH Member Conviction on Voting (Varying Windows)", xlab="Weeks Around Election", ylab="Change in 2012 turnout (percentage points)")
abline(h=0, lty=2, col=gray(.4))
segments(storage$row, storage$conv_lowCI, storage$row, storage$conv_highCI, col="dodgerblue4")
#dev.off()

#pdf("beforeafter_jail_windows40_homeowner.pdf")
plot(storage$row, storage$jail_est, ylim=c(-.3, .1), pch=19, main= "Effect of HH Member Jail Sentence on Voting (Varying Windows)", xlab="Weeks Around Election", ylab="Change in 2012 turnout (percentage points)")
abline(h=0, lty=2, col=gray(.4))
segments(storage$row, storage$jail_lowCI, storage$row, storage$jail_highCI, col="dodgerblue4")

dev.off()



weeks <- 1*40
storage <- as.data.frame(matrix(NA, nrow=weeks, ncol=13))
for (i in 2:weeks){
	lower <- ed2012 - i*7; higher <- ed2012 + i*7 #add/subtract off days to get the window
	window <- nonhomeowners[firstcase < higher & firstcase > lower, ]
	ba1 <- lm(vote2012 ~ chargebefore, data=window[anyconv==0]); summary(ba1) #est. just-charge model
	ba1.vcovCL<-cluster.vcov(ba1, window[anyconv==0]$def_spn)
	ba1conv <- lm(vote2012 ~ convbefore, data=window[anyconv==1 & anyjail==0,]); summary(ba1) #just-conv
	ba1conv.vcovCL<-cluster.vcov(ba1conv, window[anyconv==1& anyjail==0,]$def_spn)
	ba1jail <- lm(vote2012 ~ jailbefore, data=window[anyjail==1,]); summary(ba1) #jail
	ba1jail.vcovCL<-cluster.vcov(ba1jail, window[anyjail==1,]$def_spn)

	storage[i,1] <- nrow(window) 
	storage[i,2] <- est <- coeftest(ba1, ba1.vcovCL)[2,1]
	storage[i,3] <- p <- coeftest(ba1, ba1.vcovCL)[2,4]
	err<- coeftest(ba1, ba1.vcovCL)[2,2]; dof <- ba1$df
	storage[i,4] <- est + -1*err*qt(0.975, dof) 
	storage[i,5] <- est + 1*err*qt(0.975, dof) 

	storage[i,6] <- est <- coeftest(ba1conv, ba1conv.vcovCL)[2,1]
	storage[i,7] <- p <- coeftest(ba1conv, ba1conv.vcovCL)[2,4]
	err<- coeftest(ba1conv, ba1conv.vcovCL)[2,2]; dof <- ba1conv$df
	storage[i,8] <- est + -1*err*qt(0.975, dof) 
	storage[i,9] <- est + 1*err*qt(0.975, dof) 

	storage[i,10] <- est <- coeftest(ba1jail, ba1jail.vcovCL)[2,1]
	storage[i,11] <- p <- coeftest(ba1jail, ba1jail.vcovCL)[2,4]
	err<- coeftest(ba1jail, ba1jail.vcovCL)[2,2]; dof <- ba1jail$df
	storage[i,12] <- est + -1*err*qt(0.975, dof) 
	storage[i,13] <- est + 1*err*qt(0.975, dof) 
}
colnames(storage) <- c("n", "case_est", "case_p", "case_lowCI", "case_highCI", "conv_est", "conv_p", "conv_lowCI", "conv_highCI", "jail_est", "jail_p", "jail_lowCI", "jail_highCI")
storage$row <- 1:nrow(storage)

#Figure SI16
pdf("beforeafter_3panel_windows40_nonhomeowners.pdf") #stick all in one panel for now.
par(mfrow=c(3,1))
#pdf("beforeafter_justcharge_windows40_nonhomeowners.pdf")
plot(storage$row, storage$case_est, ylim=c(-.3, .1), pch=19,main= "Effect of HH Member Misdemeanor Charge on Voting (Varying Windows)", xlab="Weeks Around Election", ylab="Change in 2012 turnout (percentage points)")
abline(h=0, lty=2, col=gray(.4))
segments(storage$row, storage$case_lowCI, storage$row, storage$case_highCI, col="dodgerblue4")
#dev.off()

#pdf("beforeafter_justconv_windows40_nonhomeowners.pdf")
plot(storage$row, storage$conv_est, ylim=c(-.3, .1), pch=19, main= "Effect of HH Member Conviction on Voting (Varying Windows)", xlab="Weeks Around Election", ylab="Change in 2012 turnout (percentage points)")
abline(h=0, lty=2, col=gray(.4))
segments(storage$row, storage$conv_lowCI, storage$row, storage$conv_highCI, col="dodgerblue4")
#dev.off()

#pdf("beforeafter_jail_windows40_nonhomeowners.pdf")
plot(storage$row, storage$jail_est, ylim=c(-.3, .1), pch=19, main= "Effect of HH Member Jail Sentence on Voting (Varying Windows)", xlab="Weeks Around Election", ylab="Change in 2012 turnout (percentage points)")
abline(h=0, lty=2, col=gray(.4))
segments(storage$row, storage$jail_lowCI, storage$row, storage$jail_highCI, col="dodgerblue4")

dev.off()




# and now do a quick placebo test: look at the year before the actual election as the cutpoint? (oh, but then the "untreated" start running into actual treatment period.) what if we shift into the future? Don't want to go too far since we're losing address fidelity, but try shifting forward a year?

ed2013 <- as.Date("2013-11-05") #year after election day

weeks <- 1*40
storage <- as.data.frame(matrix(NA, nrow=weeks, ncol=13))
for (i in 2:weeks){
	lower <- ed2013 - i*7; higher <- ed2013 + i*7 #add/subtract off days to get the window
	window <- smallneighborsfull2[firstcase < higher & firstcase > lower, ]
	 #set up new treatment-timing indicators for placebo
	window[,chargebefore:=0]; window[firstcase < ed2013 & anyconv==0,chargebefore:=1] 
	window[,convbefore:=0]; window[firstcase < ed2013 & anyconv==1 & anyjail==0,convbefore:=1] 
	window[,jailbefore:=0]; window[firstcase < ed2013 & anyconv==1 & anyjail==1,jailbefore:=1] 
	ba1 <- lm(vote2012 ~ chargebefore, data=window[anyconv==0]); summary(ba1) #est. just-charge model
	ba1.vcovCL<-cluster.vcov(ba1, window[anyconv==0]$def_spn)
	ba1conv <- lm(vote2012 ~ convbefore, data=window[anyconv==1 & anyjail==0,]); summary(ba1) #just-conv
	ba1conv.vcovCL<-cluster.vcov(ba1conv, window[anyconv==1& anyjail==0,]$def_spn)
	ba1jail <- lm(vote2012 ~ jailbefore, data=window[anyjail==1,]); summary(ba1) #jail
	ba1jail.vcovCL<-cluster.vcov(ba1jail, window[anyjail==1,]$def_spn)

	storage[i,1] <- nrow(window) 
	storage[i,2] <- est <- coeftest(ba1, ba1.vcovCL)[2,1]
	storage[i,3] <- p <- coeftest(ba1, ba1.vcovCL)[2,4]
	err<- coeftest(ba1, ba1.vcovCL)[2,2]; dof <- ba1$df
	storage[i,4] <- est + -1*err*qt(0.975, dof) 
	storage[i,5] <- est + 1*err*qt(0.975, dof) 

	storage[i,6] <- est <- coeftest(ba1conv, ba1conv.vcovCL)[2,1]
	storage[i,7] <- p <- coeftest(ba1conv, ba1conv.vcovCL)[2,4]
	err<- coeftest(ba1conv, ba1conv.vcovCL)[2,2]; dof <- ba1conv$df
	storage[i,8] <- est + -1*err*qt(0.975, dof) 
	storage[i,9] <- est + 1*err*qt(0.975, dof) 

	storage[i,10] <- est <- coeftest(ba1jail, ba1jail.vcovCL)[2,1]
	storage[i,11] <- p <- coeftest(ba1jail, ba1jail.vcovCL)[2,4]
	err<- coeftest(ba1jail, ba1jail.vcovCL)[2,2]; dof <- ba1jail$df
	storage[i,12] <- est + -1*err*qt(0.975, dof) 
	storage[i,13] <- est + 1*err*qt(0.975, dof) 
}
colnames(storage) <- c("n", "case_est", "case_p", "case_lowCI", "case_highCI", "conv_est", "conv_p", "conv_lowCI", "conv_highCI", "jail_est", "jail_p", "jail_lowCI", "jail_highCI")
storage$row <- 1:nrow(storage)

#Figure SI8
pdf("beforeafter_justcharge_windows40_placebo.pdf")
plot(storage$row, storage$case_est, ylim=c(-.3, .1), pch=19,main= "Effect of HH Member Misdemeanor Charge on Voting (Varying Windows)", xlab="Weeks Around Election", ylab="Change in 2012 turnout (percentage points)")
abline(h=0, lty=2, col=gray(.4))
segments(storage$row, storage$case_lowCI, storage$row, storage$case_highCI, col="dodgerblue4")
dev.off()

pdf("beforeafter_justconv_windows40_placebo.pdf")
plot(storage$row, storage$conv_est, ylim=c(-.3, .1), pch=19, main= "Effect of HH Member Conviction on Voting (Varying Windows)", xlab="Weeks Around Election", ylab="Change in 2012 turnout (percentage points)")
abline(h=0, lty=2, col=gray(.4))
segments(storage$row, storage$conv_lowCI, storage$row, storage$conv_highCI, col="dodgerblue4")
dev.off()

pdf("beforeafter_jail_windows40_placebo.pdf")
plot(storage$row, storage$jail_est, ylim=c(-.3, .1), pch=19, main= "Effect of HH Member Jail Sentence on Voting (Varying Windows)", xlab="Weeks Around Election", ylab="Change in 2012 turnout (percentage points)")
abline(h=0, lty=2, col=gray(.4))
segments(storage$row, storage$jail_lowCI, storage$row, storage$jail_highCI, col="dodgerblue4")
dev.off()



######## check for balance in before/after on pretreat covars, etc.
# bin some pre-treat characteristics at the week level and look for discontinuities.

checkweeks <- c(-52:52)
storage <- as.data.frame(matrix(NA, nrow=length(checkweeks), ncol=11))
for (i in 1:nrow(storage)){
	week <- checkweeks[i]
	lower <- ed2012 + week*7; higher <- lower+7 #add/subtract off days to get the window
	window <- smallneighborsfull2[smallneighborsfull2$firstcase < higher & smallneighborsfull2$firstcase >= lower, ]
	#now just take some pretreatment characteristic means for this week.
	storage[i,1] <- week
	storage[i,2] <- mean(window$voter_age)
	storage[i,3] <- mean(window$voter_male, na.rm=T)
	storage[i,4] <- mean(window$vote2008)
	storage[i,5] <- mean(window$black, na.rm=T)
	storage[i,6] <- mean(window$male, na.rm=T)
	storage[i,7] <- mean(window$classA)
	storage[i,8] <- nrow(window)
	storage[i,9] <- mean(window$vote2004)
	storage[i,10] <- mean(window$vote2006)
	storage[i,11] <- mean(window$vote2010)
}

colnames(storage) <- c("week", "vage", "vmale", "vote08", "dblack", "dmale", "classA", "n", "vote04", "vote06", "vote10")
storage$row <- 1:nrow(storage)
pdf("beforeafter_checkpretreatbalance.pdf")
par(mfrow=c(2,3), oma=c(0,0,0,1), mar=c(5.1,2.1,4.1,2.1))

plot(storage$week, storage$vote08, main= "Voter 2008 Turnout", ylim=c(0,1), xlab="Weeks Around Election", cex.axis=.9, ylab="")
abline(v=0, lty=2) 
vote.lo <- loess(storage$vote08 ~ storage$week); j <- order(storage$week); lines(storage$week[j],vote.lo$fitted[j],col="red",lwd=1.5)
plot(storage$week, storage$vage, main= "Voter Age", ylim=c(30,60), xlab="Weeks Around Election", cex.axis=.9, ylab="")
abline(v=0, lty=2) 
vote.lo <- loess(storage$vage ~ storage$week); j <- order(storage$week); lines(storage$week[j],vote.lo$fitted[j],col="red",lwd=1.5)
plot(storage$week, storage$vmale, main= "Voter Gender (Male)", ylim=c(0,1), xlab="Weeks Around Election", cex.axis=.9, ylab="")
abline(v=0, lty=2) 
vote.lo <- loess(storage$vmale ~ storage$week); j <- order(storage$week); lines(storage$week[j],vote.lo$fitted[j],col="red",lwd=1.5)
plot(storage$week, storage$dblack, main= "Defendant Race (Black)", ylim=c(0,1), xlab="Weeks Around Election", cex.axis=.9, ylab="")
abline(v=0, lty=2) 
vote.lo <- loess(storage$dblack ~ storage$week); j <- order(storage$week); lines(storage$week[j],vote.lo$fitted[j],col="red",lwd=1.5)
plot(storage$week, storage$dmale, main= "Defendant Gender (Male)", ylim=c(0,1), xlab="Weeks Around Election", cex.axis=.9, ylab="")
abline(v=0, lty=2) 
vote.lo <- loess(storage$dmale ~ storage$week); j <- order(storage$week); lines(storage$week[j],vote.lo$fitted[j],col="red",lwd=1.5)
plot(storage$week, storage$classA, main= "Defendant Charge Type (Class A)", ylim=c(0,1), xlab="Weeks Around Election", cex.axis=.9, ylab="")
abline(v=0, lty=2) 
vote.lo <- loess(storage$classA ~ storage$week); j <- order(storage$week); lines(storage$week[j],vote.lo$fitted[j],col="red",lwd=1.5)
dev.off()
#Figure 1

#also, split these panels out for slides
pdf("beforeafter_checkpretreatbalance_08turnout.pdf")
plot(storage$week, storage$vote08, main= "Voter 2008 Turnout", ylim=c(0,1), xlab="Weeks Around Election", cex.axis=1.5,cex.main=2, cex.lab=1.5, ylab="")
abline(v=0, lty=2) 
vote.lo <- loess(storage$vote08 ~ storage$week); j <- order(storage$week); lines(storage$week[j],vote.lo$fitted[j],col="red",lwd=1.5)
dev.off()
pdf("beforeafter_checkpretreatbalance_voterage.pdf")
plot(storage$week, storage$vage, main= "Voter Age", ylim=c(30,60), xlab="Weeks Around Election", cex.axis=1.5,cex.main=2, cex.lab=1.5, ylab="")
abline(v=0, lty=2) 
vote.lo <- loess(storage$vage ~ storage$week); j <- order(storage$week); lines(storage$week[j],vote.lo$fitted[j],col="red",lwd=1.5)
dev.off()
pdf("beforeafter_checkpretreatbalance_votergender.pdf")
plot(storage$week, storage$vmale, main= "Voter Gender (Male)", ylim=c(0,1), xlab="Weeks Around Election", cex.axis=1.5,cex.main=2, cex.lab=1.5, ylab="")
abline(v=0, lty=2) 
vote.lo <- loess(storage$vmale ~ storage$week); j <- order(storage$week); lines(storage$week[j],vote.lo$fitted[j],col="red",lwd=1.5)
dev.off()
pdf("beforeafter_checkpretreatbalance_defrace.pdf")
plot(storage$week, storage$dblack, main= "Defendant Race (Black)", ylim=c(0,1), xlab="Weeks Around Election",cex.axis=1.5,cex.main=2, cex.lab=1.5, ylab="")
abline(v=0, lty=2) 
vote.lo <- loess(storage$dblack ~ storage$week); j <- order(storage$week); lines(storage$week[j],vote.lo$fitted[j],col="red",lwd=1.5)
dev.off()
pdf("beforeafter_checkpretreatbalance_defgender.pdf")
plot(storage$week, storage$dmale, main= "Defendant Gender (Male)", ylim=c(0,1), xlab="Weeks Around Election", cex.axis=1.5,cex.main=2, cex.lab=1.5, ylab="")
abline(v=0, lty=2) 
vote.lo <- loess(storage$dmale ~ storage$week); j <- order(storage$week); lines(storage$week[j],vote.lo$fitted[j],col="red",lwd=1.5)
dev.off()
pdf("beforeafter_checkpretreatbalance_defchargetype.pdf")
plot(storage$week, storage$classA, main= "Defendant Charge Type (Class A)", ylim=c(0,1), xlab="Weeks Around Election", cex.axis=1.5,cex.main=2, cex.lab=1.5, ylab="")
abline(v=0, lty=2) 
vote.lo <- loess(storage$classA ~ storage$week); j <- order(storage$week); lines(storage$week[j],vote.lo$fitted[j],col="red",lwd=1.5)
dev.off()


#also make one of these just of past voting
pdf("beforeafter_checkpretreatbalance_priorvoting.pdf")
par(mfrow=c(2,2))
plot(storage$week, storage$vote04, main= "Voter 2004 Turnout", ylim=c(0,1), xlab="Weeks Around Election", ylab="")
abline(v=0, lty=2) 
vote.lo <- loess(storage$vote04 ~ storage$week); j <- order(storage$week); lines(storage$week[j],vote.lo$fitted[j],col="red",lwd=1.5)

plot(storage$week, storage$vote06, main= "Voter 2006 Turnout", ylim=c(0,1), xlab="Weeks Around Election", ylab="")
abline(v=0, lty=2) 
vote.lo <- loess(storage$vote06 ~ storage$week); j <- order(storage$week); lines(storage$week[j],vote.lo$fitted[j],col="red",lwd=1.5)

plot(storage$week, storage$vote08, main= "Voter 2008 Turnout", ylim=c(0,1), xlab="Weeks Around Election", ylab="")
abline(v=0, lty=2) 
vote.lo <- loess(storage$vote08 ~ storage$week); j <- order(storage$week); lines(storage$week[j],vote.lo$fitted[j],col="red",lwd=1.5)

plot(storage$week, storage$vote10, main= "Voter 2010 Turnout", ylim=c(0,1), xlab="Weeks Around Election", ylab="")
abline(v=0, lty=2) 
vote.lo <- loess(storage$vote10 ~ storage$week); j <- order(storage$week); lines(storage$week[j],vote.lo$fitted[j],col="red",lwd=1.5)
dev.off()
#Figure SI1


#and just run some regressions as well. 
lower <- ed2012 - 40*7; higher <- ed2012 + 40*7 #add/subtract off days to get a 40-week window (comparable to the main plots)
window <- smallneighborsfull2[firstcase < higher & firstcase > lower, ]
dim(window) #40 weeks

lm1 <- lm(vote2008 ~ jailbefore, data=window[anyjail==1,]); summary(lm1)
lm1 <- lm(vote2010 ~ jailbefore, data=window[anyjail==1,]); summary(lm1)
lm1 <- lm(as.numeric(voter_age) ~ jailbefore, data=window[anyjail==1,]); summary(lm1)
lm1 <- lm(voter_male ~ jailbefore, data=window[anyjail==1,]); summary(lm1)
lm1 <- lm(male ~ jailbefore, data=window[anyjail==1,]); summary(lm1)
lm1 <- lm(black ~ jailbefore, data=window[anyjail==1,]); summary(lm1)

#and plot just how many registered voters are getting matched to people facing charges, over time
pdf("beforeafter_checkmatchbalance.pdf") 
plot(storage$week, storage$n, main= "Number of Registered Voters Matched", ylim=c(0,330), xlab="Arrest Date: Weeks From Election", ylab="Number of Voters Matched to Defendants", cex.main=1.5, cex.axis=1.5,cex=1.5, cex.lab=1.45)
abline(v=0, lty=2) 
match.lo <- loess(storage$n ~ storage$week)
j <- order(storage$week)
lines(storage$week[j],match.lo$fitted[j],col="red",lwd=3)
dev.off()
#Figure SI4



